import 'package:flutter/material.dart';
import 'package:mall/constant/app_colors.dart';
import 'package:mall/constant/app_dimens.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/text_style.dart';
import 'package:mall/modules/business.dart';
import 'package:mall/ui/widgets/loading_dialog.dart';
import 'package:mall/ui/widgets/fm_icon.dart';
import 'package:mall/utils/ext/widget_ext.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class RegisterPage extends StatelessWidget {
  final GlobalKey<FormState> _registerKey = GlobalKey<FormState>();
  final TextEditingController _accountController = TextEditingController();
  final TextEditingController _passWordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.C_FFFFFF,
      appBar: AppBar(
        title: Text(AppStrings.REGISTER),
        centerTitle: true,
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Container(
            alignment: Alignment.centerLeft,
            margin: EdgeInsets.only(left: 22.5, right: 22.5, top: ScreenUtil().setWidth(AppDimens.DIMENS_160)),
            child: Form(
                key: _registerKey,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      AppStrings.REGISTER_WELCOME,
                      style: FMTextStyle.color_333333_size_22,
                    ),
                    Padding(padding: EdgeInsets.only(top: 10)),
                    Text(
                      AppStrings.LOGIN_APP_INTRODUCE,
                      style: FMTextStyle.color_999999_size_12,
                    ),
                    Padding(padding: EdgeInsets.only(top: ScreenUtil().setHeight(AppDimens.DIMENS_100))),
                    TextFormField(
                      maxLines: 1,
                      maxLength: 11,
                      keyboardType: TextInputType.phone,
                      validator: _validatorAccount,
                      decoration: InputDecoration(
                        icon: Icon(
                          Icons.account_circle,
                          color: AppColors.C_999999,
                          size: ScreenUtil().setWidth(AppDimens.DIMENS_80),
                        ),
                        hintText: AppStrings.ACCOUNT_HINT,
                        hintStyle: FMTextStyle.color_999999_size_12,
                        labelStyle: FMTextStyle.color_222222_size_13,
                        labelText: AppStrings.ACCOUNT,
                      ),
                      controller: _accountController,
                    ),
                    Padding(padding: EdgeInsets.only(top: 10)),
                    TextFormField(
                      maxLines: 1,
                      maxLength: 12,
                      obscureText: true,
                      validator: _validatorPassWord,
                      decoration: InputDecoration(
                        icon: Icon(
                          FMICon.PASS_WORD,
                          color: AppColors.C_999999,
                          size: ScreenUtil().setWidth(AppDimens.DIMENS_80),
                        ),
                        hintText: AppStrings.PASSWORD_HINT,
                        hintStyle: FMTextStyle.color_999999_size_12,
                        labelStyle: FMTextStyle.color_222222_size_13,
                        labelText: AppStrings.PASSWORD,
                      ),
                      controller: _passWordController,
                    ),
                    Padding(padding: EdgeInsets.only(top: 27.5)),
                    Container(
                      alignment: Alignment.center,
                      width: double.infinity,
                      height: 45,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(AppDimens.DIMENS_30)),
                        color: AppColors.C_FF5722,
                      ),
                      child: Text(AppStrings.LOGIN, style: FMTextStyle.color_ffffff_size_13),
                    ).onInkwellTap(() => _register(context)),
                  ],
                )),
          ),
        ),
      ),
    );
  }

  void _register(BuildContext context) {
    if (_registerKey.currentState!.validate()) {
      _showRegisterDialog(context);
      // _registerViewModel.register(_accountController.text, _passWordController.text).then((response) {
      //   Navigator.pop(context);
      //   if (response) {
      //     HUDUtil.toast(AppStrings.REGISTER_SUCCESS);
      //     Navigator.pop(context);
      //   }
      // });
      Business.register(_accountController.text, _passWordController.text).then((response) {
        Navigator.pop(context);
      });
    }
  }

  _showRegisterDialog(BuildContext context) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return LoadingDialog(
            title: AppStrings.LOGINING,
            textColor: AppColors.C_999999,
            titleSize: AppDimens.DIMENS_13,
            indicatorRadius: 22.5,
          );
        });
  }

  String? _validatorAccount(String? value) {
    if (value != null && value.length < 11) {
      return AppStrings.ACCOUNT_RULE;
    }
    return null;
  }

  String? _validatorPassWord(String? value) {
    if (value != null && value.length < 6) {
      return AppStrings.PASSWORD_HINT;
    }
    return null;
  }
}
